﻿/******************************************************************************
 * Copyright (C) 671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef GAME_SERVER_AUTHSESSIONMANAGER_H_
#define GAME_SERVER_AUTHSESSIONMANAGER_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <ctime>
#include <boost/unordered_map.hpp>
#include <NodeCpp/Macros.h>
#include <NodeCpp/Singleton.h>
#include <NodeCpp/ThreadingModel.h>
#include <NodeCpp/Mutex.h>
#include <NodeCpp/LockedQueue.h>
using NodeCpp::Singleton;
using NodeCpp::ClassLevelLockable;
using NodeCpp::SingleThreadMutex;
using NodeCpp::Mutex;
using NodeCpp::LockedQueue;

class WorldSocketDelegate;
class AuthSession;

class AuthSessionManager
{
public:
    AuthSessionManager(void);
    ~AuthSessionManager(void);

    AuthSession* NewSession(WorldSocketDelegate* _Sock);
    void Update(time_t _Diff);
    void Close(void);

    bool IsRunning(void) const { return Running_; }
    void SetMaxUnauthedTime(time_t _Time) { MaxUnauthedTime_ = _Time; }
    time_t GetMaxUnauthedTime(void) const { return MaxUnauthedTime_; }

    AuthSession* FindSession(int _ID);

private:
    typedef LockedQueue<AuthSession*> NewSessionQueue;
    typedef boost::unordered_map<int, AuthSession*> SessionMap;

    NewSessionQueue NewSessions_;
    SessionMap Sessions_;
    bool Running_;

    time_t MaxUnauthedTime_;
    DISALLOW_COPY_AND_ASSIGN(AuthSessionManager);
};
#define sAuthSessionMgr NodeCpp::Singleton<AuthSessionManager>::Instance()

#endif // GAME_SERVER_AUTHSESSIONMANAGER_H_
